package com.shiku.imserver.message.hander;

import com.shiku.imserver.CoreService;
import com.shiku.imserver.common.annotation.MessageCommandAnnotation;
import com.shiku.imserver.common.constant.KConstants;
import com.shiku.imserver.common.hander.AbstractMessageHandler;
import com.shiku.imserver.common.message.AbstractBaseMessage;
import com.shiku.imserver.common.message.ChatMessage;
import com.shiku.imserver.common.message.PacketVO;
import com.shiku.imserver.common.message.PullBatchGroupMessage;
import com.shiku.imserver.common.message.PullMessageHistoryRecord;
import com.shiku.imserver.common.message.PullMessageHistoryRecordResp;
import com.shiku.imserver.common.message.SuccessMessage;
import com.shiku.imserver.common.packets.ImPacket;
import com.shiku.imserver.common.proto.MessageProBuf;
import com.shiku.imserver.common.utils.ProBufUtils;
import com.shiku.imserver.message.MessageFactory;
import com.shiku.imserver.service.IMBeanUtils;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;

public class PullMessageHandler extends AbstractMessageHandler {
    private static final Logger logger = LoggerFactory.getLogger(PullMessageHandler.class);

    @MessageCommandAnnotation(command = 12)
    public PacketVO pullMessageHandler(ImPacket packet, ChannelContext channelContext) {
        List<ChatMessage> messageList;
        try {
            PullMessageHistoryRecord message = (PullMessageHistoryRecord) ProBufUtils.decoderMessageBody(packet.getBytes(), MessageProBuf.PullMessageHistoryRecordReqProBuf.getDescriptor(), PullMessageHistoryRecord.class);
            String from = message.getMessageHead().getFrom();
            String userId = CoreService.parseBareUserId(from);
            String jid = message.getJid();
            int size = message.getSize();
            long startTime = message.getStartTime();
            long endTime = message.getEndTime();
            if (KConstants.isDebug) {
                logger.info("pull message userId > {}  jid {} size {} startTime {} endTime {}", new Object[]{from, jid, Integer.valueOf(size), Long.valueOf(startTime), Long.valueOf(endTime)});
            }
            if (1 == message.getMessageHead().getChatType()) {
            	logger.info("[ww]:PullMessageHandler pullChatHistoryMessage");
                messageList = IMBeanUtils.getMessageRepository().pullChatHistoryMessage(userId, jid.replace("_", "/"), size, startTime, endTime);
            } else {
            	logger.info("[ww]:PullMessageHandler pullGroupHistoryMessage");
                messageList = IMBeanUtils.getMessageRepository().pullGroupHistoryMessage(jid, size, startTime, endTime);
            }
            PullMessageHistoryRecordResp historyRecordResp = new PullMessageHistoryRecordResp();
            historyRecordResp.setMessageId(message.getMessageHead().getMessageId());
            historyRecordResp.setChatType(message.getMessageHead().getChatType());
            historyRecordResp.setJid(message.getJid());
            historyRecordResp.setMessageList(messageList);
            ImPacket imPacket = ProBufUtils.encodeImPacket(historyRecordResp, MessageProBuf.PullMessageHistoryRecordRespProBuf.getDescriptor());
            imPacket.setCommand((short)13);
            Tio.send(channelContext, imPacket);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @MessageCommandAnnotation(command = 14)
    public PacketVO pullBatchGroupMessageHandler(ImPacket packet, ChannelContext channelContext) throws Exception {
        try {
            PullBatchGroupMessage message = (PullBatchGroupMessage) ProBufUtils.decoderMessageBody(packet.getBytes(), MessageProBuf.PullBatchGroupMessageReqProBuf.getDescriptor(), PullBatchGroupMessage.class);
            logger.info("[ww]:PullMessageHandler pullBatchGroupMessageHandler");
            Tio.send(channelContext, MessageFactory.createSuccessIMPacket(message));
            IMBeanUtils.getMessageRepository().pullBatchGroupHistoryMessage(channelContext, message);
            
	          ImPacket imPacketResult = null; 
	      	  SuccessMessage result = new SuccessMessage();
	      	  result.setMessageHead( MessageFactory.createMessageHead(channelContext));
	      	  result.getMessageHead().setChatType((byte)10);
	      	  imPacketResult = ProBufUtils.encodeImPacket((AbstractBaseMessage)result, MessageProBuf.CommonSuccessProBuf.getDescriptor());
	      	  imPacketResult.setCommand((short)102);
	      	  CoreService.send(channelContext, imPacketResult);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @MessageCommandAnnotation(command = 16)
    public PacketVO pullBatchGroupMessageLimitHandler(ImPacket packet, ChannelContext channelContext) throws Exception {
        try {
            PullBatchGroupMessage message = (PullBatchGroupMessage) ProBufUtils.decoderMessageBody(packet.getBytes(), MessageProBuf.PullBatchGroupMessageReqProBuf.getDescriptor(), PullBatchGroupMessage.class);
            logger.info("[ww]:PullMessageHandler pullBatchGroupMessageHandler");
            Tio.send(channelContext, MessageFactory.createSuccessIMPacket(message));
            IMBeanUtils.getMessageRepository().pullBatchGroupHistoryMessage(channelContext, message);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
